extends CharacterBody2D

const SPEED = 300.0
var JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
# self var
var JUMP_TIMES = 0
var tab_status = 0
var flip_v_status = false

@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var staring_position= global_position
@onready var color_rect = $Camera2D/ColorRect
@onready var camera_2d = $Camera2D

@onready var player_collision = $player_collision
@onready var danger_collision = $danger_area/danger_collision
@onready var source_collision = $source_area/source_collision
@onready var outdoor_collision = $next_area/outdoor_collision
@onready var pre_collision = $pre_area/pre_collision

@onready var player = $"."

func _physics_process(delta):
	if flip_v_status==false:
		# Add the gravity.
		if not is_on_floor():
			velocity.y += gravity * delta
		# self Handle jump.
		if Input.is_action_just_pressed("ui_accept"):
			if is_on_floor():
				if JUMP_TIMES==0:     
					velocity.y += JUMP_VELOCITY
					JUMP_TIMES+=1
			else:	
				if JUMP_TIMES==0:
					velocity.y = 0
					velocity.y += JUMP_VELOCITY
					JUMP_TIMES+=1
				elif JUMP_TIMES==1:
					velocity.y = 0
					velocity.y += JUMP_VELOCITY
					JUMP_TIMES+=1
		else:
			if is_on_floor():
				JUMP_TIMES=0
	else:
		# Add the gravity.
		if not is_on_ceiling():
			velocity.y += gravity * delta
		# self Handle jump.
		if Input.is_action_just_pressed("ui_accept"):
			if is_on_ceiling():
				if JUMP_TIMES==0:     
					velocity.y += JUMP_VELOCITY
					JUMP_TIMES+=1
			else:	
				if JUMP_TIMES==0:
					velocity.y = 0
					velocity.y += JUMP_VELOCITY
					JUMP_TIMES+=1
				elif JUMP_TIMES==1:
					velocity.y = 0
					velocity.y += JUMP_VELOCITY
					JUMP_TIMES+=1
		else:
			if is_on_ceiling():
				JUMP_TIMES=0
				
				
	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	updata_animations(direction)
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	move_and_slide()

func updata_animations(input_axis):
	if flip_v_status==false:
		if input_axis!=0:
			animated_sprite_2d.flip_h=(input_axis<0)
			animated_sprite_2d.play("run")
		else:
			animated_sprite_2d.play("idle")
		if not is_on_floor():
			animated_sprite_2d.play("jump")
	else:
		if input_axis!=0:
			animated_sprite_2d.flip_h=(input_axis<0)
			animated_sprite_2d.play("run")
		else:
			animated_sprite_2d.play("idle")
		if not is_on_ceiling():
			animated_sprite_2d.play("jump")
@onready var progress_bar = $ProgressBar

func meet_heart(area):
	progress_bar.value+=20
	pass # Replace with function body.


func meet_danger(area):
	progress_bar.value-=20
	if progress_bar.value==0:
		global_position=staring_position
		progress_bar.value=100
	pass # Replace with function body.

func succeed(area):
	Event.level_next.emit()
	pass # Replace with function body.



func _input(event):
	if event.is_action_pressed("open_tab") :
		if tab_status==0:
			tab_status=1
			color_rect.show()
			print(Event.rucksack_prop)
		else:
			tab_status=0
			color_rect.hide()
		
		print("my_action occurred!")


func _on_ready():
	
	Event.player_change_big.connect(player_big)
	Event.player_change_small.connect(player_small)
	Event.player_antigravity.connect(player_antigravity)
	Event.player_use_bow.connect(player_add_bow)
	pass # Replace with function body.
	
func player_big():
	player.scale[0]*=2
	player.scale[1]*=2
	camera_2d.zoom[0]/=2
	camera_2d.zoom[1]/=2
	pass
	
func player_small():
	player.scale[0]/=2
	player.scale[1]/=2
	camera_2d.zoom[0]*=2
	camera_2d.zoom[1]*=2
	pass


func pre_level(area):
	Event.level_pre.emit()
	
	pass # Replace with function body.

func player_antigravity():
	gravity*=-1
	JUMP_VELOCITY*=-1
	if flip_v_status==false:
		flip_v_status=true
	else:
		flip_v_status=false	
	animated_sprite_2d.flip_v = flip_v_status
	pass
@onready var bow = $Bow

func player_add_bow():
	bow.show()
	pass
